package com.example.fist.dao;

import com.example.fist.entity.DetailsEntity;
import com.example.fist.personRepository.AdminJpaRepository;
import com.example.fist.personRepository.DetailsJpaRepository;
import com.example.fist.util.Json.ErrorJson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DetailsDao {
    @Autowired
    private DetailsJpaRepository detailsRepository;

    public DetailsDao(DetailsJpaRepository detailsRepository) {
        this.detailsRepository=detailsRepository;
    }


    public  int addDetails(DetailsEntity de){
        DetailsEntity d=  this.detailsRepository.save(de);
        if(d!=null){
        return  1;

        }
        return 0;
    }

    public  DetailsEntity get(int id){
return  detailsRepository.get(id);
    }

    public  int deletDetails(int id){
          detailsRepository.deleteById(id);
          return  1;
    }

    public String findAllPage(int page){

        int pagesize=2;
        int count=detailsRepository.findAllCount();
        int countPage=0;
        if(count%10==0){
            countPage=count/pagesize;
        }
        else{
            countPage=count/pagesize+1;
        }
        if(page>countPage||count<=0){
                return new  ErrorJson(4001,"页码不在范围:"+page).toString();
        }

        Pageable pageable = PageRequest.of(page-1,pagesize);
        Page<DetailsEntity> pa=detailsRepository.findAllPage(pageable);


        ObjectMapper mapper = new ObjectMapper();
        try {
            String j = mapper.writeValueAsString(pa);
            JSONObject json=new JSONObject();


            try {
                JSONObject jss=new JSONObject(j);

                json.put("count",count);
                json.put("countpage",countPage);
                json.put("page",page);

                json.put("messg","ok");
                json.put("code",200);

                JSONArray ja=jss.getJSONArray("content");

                for(int i=0;i<ja.length();i++){
                    JSONObject jt=ja.getJSONObject(i);
                    String st=jt.remove("modifiTime").toString();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                    long lt = new Long(st);
                    Date date = new Date(lt);
                    jt.put("modifiTime",df.format(date));

                }
                json.put("data",ja);
                return  json.toString();
            } catch (JSONException e) {
                e.printStackTrace();
                return new ErrorJson(4002,"").toString();
            }


        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new ErrorJson(4003,"").toString();
        }


    }
}
